package cn.itcast.web04.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestParameter
 */
public class RequestParameter extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset = utf-8");
	    
	    // 1、根据name属性值获取用户输入的值
	    String username = request.getParameter("username");
	    String password = request.getParameter("password");
	    
	    // 2、获取用户选择的值
	    String[] hobbies = request.getParameterValues("hobby");
	    PrintWriter out = response.getWriter();
	    out.println("用户名：" + username + "<br>密码："
	    + password + "<br>爱好:<br>" + Arrays.toString(hobbies));
	    
	    // 3、没有参数，获取所有的值
	    Map<String, String[]> map = request.getParameterMap();
	    // 根据keySet()获取所有的key
	    Set<String> set = map.keySet();
	    for (String key : set) {
	        String[] values = map.get(key);
	        System.out.println(key + " = " + Arrays.toString(values));
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
